﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Super.Common.Validate
{
    /// <summary> 数据验证元素 (单一规则 比如 不能为空)
    /// </summary>
    public class ValidateElement
    {
        /*
        /// <summary> 构造多参数的ValidateElement (Func 好像不能重载)
        /// </summary>
        /// <returns></returns>
        public static ValidateElement CreateMultiParameter(string type, string parameter, string errorMessage, Func<System.Web.HttpRequest, bool> validateMethod)
        {
            return new ValidateElement(type, parameter, errorMessage) { ValidateMethodByMultiParameter = validateMethod };
        }
         */

        /// <summary> 构造远端验证.
        /// </summary>
        /// <returns></returns>
        public static ValidateElement CreateRemoteValidate(string type, string parameter, Func<string, object> validateMethod)
        {
            return new ValidateElement(type, parameter, null) { ValidateMethodByRemote = validateMethod };
        }

        public ValidateElement(string type, string parameter, string errorMessage, Func<string, bool> validateMethod)
            : this(type, parameter, errorMessage, (j, k) => validateMethod(j))
        { }

        public ValidateElement(string type, string parameter, string errorMessage, Func<string, string, bool> validateMethod)
            : this(type, parameter, errorMessage)
        {
            this.ValidateMethod = validateMethod;
        }

        private ValidateElement(string type, string parameter, string errorMessage)
        {
            this.Type = type;
            this.Parameter = parameter;
            this.ErrorMessage = errorMessage;
        }

        /// <summary> 验证类型
        /// </summary>
        public string Type { get; private set; }

        /// <summary> 验证参数
        /// </summary>
        public string Parameter { get; private set; }

        /// <summary> 验证并返回是否通过 第一个参数为控件值,第二个参数为比对参数
        /// </summary>
        public Func<string, string, bool> ValidateMethod { get; private set; }

        /// <summary> 验证并返回结果. 用于远端验证规则.
        /// </summary>
        public Func<string, object> ValidateMethodByRemote { get; private set; }

        /*
        /// <summary> 验证并返回是否通过 第一个参数为控件值,第二个参数为比对参数
        /// </summary>
        public Func<System.Web.HttpRequest, bool> ValidateMethodByMultiParameter { get; private set; }
         */

        /// <summary> 验证未通过消息 
        /// </summary>
        public string ErrorMessage { get; private set; }

        new public string ToString()
        {
            return this.Type + ":" + this.Parameter + " message:" + this.ErrorMessage;
        }
    }
}
